package com.mogujie.tt.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.mogujie.tt.R;
import com.mogujie.tt.ui.activity.ImagePagerActivity;
import com.mogujie.tt.ui.widget.QuanzQzPhone;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import net.tsz.afinal.FinalBitmap;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 50281 on 2015/9/15.
 */
public class QuanzQzImageAdapter extends BaseAdapter {
    private Context context;
    private List paths;
    private boolean localtion;// true是本地图片 false 是网络图片
    private FinalBitmap finalBit;

    private AddImage add;

    public QuanzQzImageAdapter(Context context, List paths, boolean localtion) {
        this.context = context;
        this.paths = paths;
        this.localtion = localtion;
        finalBit = FinalBitmap.create(context);
    }

    public void setAddImage(AddImage add) {
        this.add = add;
    }


    public void setData(List<String> paths) {
        this.paths = paths;
    }

    @Override
    public int getCount() {
        return paths == null ? 0 : paths.size();
    }

    @Override
    public Object getItem(int position) {
        return paths == null ? null : paths.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder = new Holder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.quanz_qz_image, null);
            holder.imageView = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> urls1=new ArrayList<String>();
                for (int i=0;i<paths.size();i++){
                    QuanzQzPhone urls2 = (QuanzQzPhone)paths.get(i);
                    urls1.add(urls2.max);
                }
                Intent intent = new Intent(context, ImagePagerActivity.class);
                // 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls1);
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX,position);
                context.startActivity(intent);
            }
        });
        QuanzQzPhone ph = (QuanzQzPhone) paths.get(position);
        finalBit.display(holder.imageView, ph.min);
        DisplayImageOptions options = new DisplayImageOptions.Builder()//
                .showImageOnLoading(R.drawable.ic_launcher) // 加载中显示的默认图片
                .showImageOnFail(R.drawable.ic_launcher) // 设置加载失败的默认图片
                .cacheInMemory(true)//
                .cacheOnDisk(true)//
                .bitmapConfig(Bitmap.Config.RGB_565)//
                .build();
        ImageLoader.getInstance().displayImage(((QuanzQzPhone) paths.get(position)).min, holder.imageView, options);
        return convertView;
    }

    private class Holder {
        public ImageView imageView;
    }

    /**
     * 加载图片接口
     *
     * @author jiangyue
     *
     */
    public interface AddImage {
        public void addImage(ImageView view, String path);
    }
}
